import {openid,code} from './urlconfig/config.js';
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串

    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

//发送日志
function sendTagLog(keyword,async){
    $.ajax({
        type: "post",
        url: "/weixin/product/sendTagLog",
        async:async,
        data: {"token":webuser.data.token,"keyword":keyword},
        dataType: "json",
        success: function(data){

        }
    });
}
/* ***************************************
*
*	JS 微信登录
*
******************************************/
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串

    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
// (function() {
var canshu = GetRequest(); //获取get参数

//判断用户是否已关注公众号 （防止没有关注公众号用户进入获取不到用户信息）
if(!canshu['is_guanzhu']){
    var is_guanzhu = 1; //已关注
}else{
    var is_guanzhu = canshu['is_guanzhu']; //已关注
}

//$.cookie('webuser',null,{path:'/'}); //清空cookie
if($.cookie('webuser')){
    var webuser = JSON.parse($.cookie('webuser')); //获取cookie中的用户信息
}else{
    var webuser = ''; //获取cookie中的用户信息
}

//判断是否已登录
if(webuser){
    if(webuser['code'] != 200){
        if(canshu['code']){
            openid(canshu['code'],is_guanzhu);
        }else{
            code(is_guanzhu,is_guanzhu);
        }
    }else if(typeof(webuser.data.openid) == "undefined"){
        $.cookie('webuser',null,{path:'/'}); //清空cookie
        window.location.reload();
    }

}else{
    if(canshu['code']){
        openid(canshu['code'],is_guanzhu);
    }else{
        code(is_guanzhu);
    }
}

/* *************************
*
*	重定向获取code
*
****************************/

function code(is_guanzhu){
    //获取url

    // 处理回调url
    var dq_url = window.location.href; //获取当前url
    var mdarr = dq_url.split('#'); //url参数
    dq_url = mdarr[0];
    var arr = dq_url.split('?'); //url参数
    dq_url = arr[0];
    var aaa = arr[1];
    if(aaa){
        var arr_2 = aaa.split('is_guanzhu');
        arr_2 = arr_2[0].split('#');
        if(arr_2[0]){
            dq_url = dq_url+"?"+arr_2[0]+"&is_guanzhu="+is_guanzhu;
        }else{
            dq_url = dq_url+"?is_guanzhu="+is_guanzhu;
        }
    }else{
        dq_url = dq_url+"?is_guanzhu="+is_guanzhu;
    }

    if(mdarr[1]){
        dq_url = dq_url+"#"+mdarr[1];
    }
    // alert(dq_url);
    dq_url = encodeURIComponent(dq_url);
    //接口url
    var url = code+"?is_guanzhu="+is_guanzhu+"&url="+dq_url;
    //重定向url
    window.location.href = url;
}


/* *************************
*
*	获取用户基本信息
*
****************************/
function openid(code_num,is_guanzhu){
    var dq_url = window.location.href;

    var arr = dq_url.split('?');
    dq_url = arr[0];

    //请求获取openID接口
    $.ajax({
        type: "post",
        url: openid,
        async:false, //同步
        data: {"code":code_num,"url":dq_url,"is_guanzhu":is_guanzhu},
        dataType: "json",
        success: function(data){
            if(data.code == 200){
                webuser = data;
                var user_str = JSON.stringify(data);

                $.cookie('webuser',user_str,{path:'/'});
            }else{
                code(0);
            }

        }
    });

}
